<template>
    <el-dialog title="选择专题" :visible.sync="showDialogPop" class="el-dialog-edit">
        <el-form label-position="left" label-width="100px" style="width: 100%; padding-left: 20px;">
            <el-form-item label="所选专题：" v-if="count !== 1">
                <div class="el-tag-box">
                    <el-tag v-for="(item, index) of itemCheck" :key="item.id" :closable="true" @close="delItem(index)">{{item.name}}</el-tag>
                </div>
            </el-form-item>
        </el-form>
        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border @row-click="clickRow">
            <el-table-column align="center" width="50">
                <template slot-scope="scope">
                    <label class="input-radio" v-if="count === 1">
                        <input type="radio" :value="{id: scope.row.topicId, name: scope.row.name}" v-model="itemRadio"/>
                        <span></span>
                    </label>
                    <label class="input-checkbox" v-else>
                        <input type="checkbox" :value="{id: scope.row.topicId, name: scope.row.name}" v-model="itemCheck"/>
                        <span></span>
                    </label>
                </template>
            </el-table-column>
            <el-table-column align="center" label="专题名称" prop="name"></el-table-column>
            <el-table-column align="center" label="专题Logo">
                <template slot-scope="scope">
                    <div class="img-box" style="border: 0;margin: 0 auto">
                        <img :src="scope.row.image"/>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @current-change="handleCurrentChange"
                :current-page.sync="listQuery.pageIndex"
                :page-size="listQuery.pageSize"
                background
                layout="total, prev, pager, next, jumper"
                style="text-align: center"
                :total="listTotal">
        </el-pagination>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="btnLoading" @click="chooseSpecial">确定</el-button>
            <el-button @click="close">取消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    export default {
        name: 'chooseSpecial',
        props: {
            check: {
                type: Array,
                default() {
                    return [];
                }
            },
            count: {
                type: Number,
                default: -1
            }
        },
        data() {
            return {
                showDialogPop: true,
                btnLoading: false,
                itemRadio: {},
                itemCheck: this.check,

                listQuery: {
                    // name: '',
                    pageIndex: 1,
                    pageSize: 6,
                    isPage: 1
                },
                listLoading: false,
                list: [],
                listTotal: 0,
            }
        },
        created() {
            this.getList();
            if (this.count === 1 && this.check.length > 0) {
                this.itemRadio = this.check[0];
            }
        },
        methods: {
            // 获取列表
            getList() {
                this.listLoading = true;
                this.request({
                    url: '/bsnl-activity/specialTopic/list',
                    method: 'get',
                    params: this.listQuery
                }).then(({data, totalCount}) => {
                    this.list = data;
                    this.listTotal = totalCount;
                    this.listLoading = false;
                }).catch(err => {
                    this.list = [];
                    this.listTotal = 0;
                    this.listLoading = false;
                });
            },
            // 单击行
            clickRow({topicId, name}) {
                if (this.count === 1) {
                    this.itemRadio = {
                        id: topicId,
                        name
                    };
                    return false;
                }
                let index = this.itemCheck.findIndex(item => item.id === topicId);
                if (index < 0) {
                    this.itemCheck.push({
                        id: topicId,
                        name
                    });
                } else {
                    this.itemCheck.splice(index, 1);
                }
            },
            // 通过tag删除
            delItem(index) {
                this.itemCheck.splice(index, 1);
            },
            // 选择专题
            chooseSpecial() {
                this.btnLoading = true;
                if (this.count === 1) {
                    this.itemCheck = [this.itemRadio]
                }
                if (this.itemCheck.length < 1) {
                    return this.$message.error('请选择专题');
                }
                if(this.count > 0 && this.itemCheck.length > this.count){
                    return this.$message.error(`最多选择${this.count}个专题`);
                }
                this.btnLoading = false;
                this.$emit('choose', this.itemCheck);
                this.close();
            },
            // 关闭弹窗
            close() {
                this.showDialogPop = false;
                document.body.removeChild(this.$el);
            },
            // 搜索
            search() {
                this.listQuery.pageIndex = 1;
                this.getList();
            },
            // 切换分页
            handleCurrentChange(page) {
                this.listQuery.pageIndex = page;
                this.getList();
            },
        }
    }
</script>
